import { Component, OnInit, Input } from '@angular/core';

import { AdvertiseService } from '../../../services/advertise.service';
import { aroAnimation } from '../../../animation';

@Component({
  selector: 'app-home-slider',
  templateUrl: './home-slider.component.html',
  styleUrls: ['./home-slider.component.css'],
  animations: [aroAnimation]
})
export class HomeSliderComponent implements OnInit {

  public currentImg: number = 0;
  protected adTimer: any;

  @Input()
  public imgs: Array<any> = JSON.parse(localStorage.getItem('BannerImage')) || [];

  constructor(private advertiseService: AdvertiseService) {
    this.adTimer = setInterval(() => {
      let id = (this.currentImg + 1) % 4;
      this.currentImg = id;
    }, 3000);
  }

  ngOnInit() {
    this.advertiseService.getList().subscribe(res => {
      if (res.Code == 100) {
        this.imgs = res.Data;
        // 存入local
        localStorage.setItem('BannerImage', JSON.stringify(this.imgs));
      }
    });
  }

  ngOnDestroy(): void {
    clearInterval(this.adTimer);
  }
}
